package com.haipumi.back.actions;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.haipumi.beans.DataGrid;
import com.haipumi.beans.haipumi_activity;
import com.haipumi.biz.IBackActivityBiz;
import com.haipumi.biz.impl.BackActivityBizImpl;
import com.haipumi.utils.RequestUtil;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;


@Controller("backActiveAction")
public class BackActiveAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -620499125809585820L;

	@Autowired
	IBackActivityBiz backActivityBiz;
	
	haipumi_activity backactivity;
	
	DataGrid dg=new DataGrid();
	
	public void setDg(DataGrid dg) {
		this.dg = dg;
	}

	public DataGrid getDg() {
		return dg;
	}

	public String doActives(){
		//先从request里获取op选项为多少
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpServletResponse response=ServletActionContext.getResponse();
		String op=request.getParameter("op");
		if("findallactivity".equals(op)){
			findallactivity(request,response);
		}else if("addactivity".equals(op)){
			addactivity(request,response);
		}else if("updateactivity".equals(op)){
			updateactivity(request,response);
		}
		return Action.SUCCESS;
	}

	private void updateactivity(HttpServletRequest request,
			HttpServletResponse response) {
		haipumi_activity activity=RequestUtil.parseRequest(request, haipumi_activity.class);
		backactivity=backActivityBiz.editActivity(activity);
		dg.setRows(backActivityBiz.findAllActivity());
		dg.setRows(backActivityBiz.countAllActivity());
	}

	private void addactivity(HttpServletRequest request,
			HttpServletResponse response) {
		haipumi_activity activity=RequestUtil.parseRequest(request, haipumi_activity.class);
		if(!"".equals(activity.getAcname())||!"".equals(activity.getAcstarttime())||!"".equals(activity.getAcstatus())){
			return ;
		}
		backactivity=backActivityBiz.addActivity(activity);
		dg.setRows(backActivityBiz.findAllActivity());
		dg.setRows(backActivityBiz.countAllActivity());
	}

	private void findallactivity(HttpServletRequest request,
			HttpServletResponse response) {
		List<haipumi_activity>activitylist=null;
		activitylist=backActivityBiz.findAllActivity();
		int r=0;
		r=backActivityBiz.countAllActivity();
		dg.setRows(activitylist);
		dg.setTotal(r);
	}
}
